package com.ruoyi.platform.system.menu.service;

import com.ruoyi.base.permission.TokenManager;
import com.ruoyi.base.service.BaseService;
import com.ruoyi.platform.system.menu.manager.MenuManager;
import com.ruoyi.platform.system.menu.pojo.MenuTreeDTO;
import com.ruoyi.platform.system.role.dao.UserRoleDAO;
import com.ruoyi.platform.system.role.enums.RoleEnum;
import com.ruoyi.platform.system.role.pojo.UserRoleDO;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 菜单
 *
 * @author lcf
 */
@Service
public class MenuService extends BaseService {
    @Autowired
    private UserRoleDAO userRoleDAO;
    @Autowired
    private TokenManager tokenManager;

    /**
     * 获取用户菜单列表
     */
    public List<MenuTreeDTO> getUserMenuList() {
        List<UserRoleDO> userRoleList = userRoleDAO.selectByUserIdList(Collections.singletonList(tokenManager.getLoginUserId()));
        if (CollectionUtils.isEmpty(userRoleList)) {
            return MenuManager.getMenuList();
        }

        List<RoleEnum> roleList = userRoleList.stream().map(UserRoleDO::getRoleCode).map(RoleEnum::ofCode).filter(Objects::nonNull).collect(Collectors.toList());
        return MenuManager.getMenuList(roleList);
    }
}
